﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using IndexMarkSilverlightClient.Classes;
using IndexMarkSilverlightClient.IndexMarkServiceReference;

namespace IndexMarkSilverlightClient.Views.Controls.ReportsControls
{
    public partial class VolumsPPSView : UserControl, INotifyPropertyChanged
    {
        private IndexMarkServiceReference.IndexMarkServiceClient client = new IndexMarkServiceClient();
        private ObservableCollection<IndexRow> _indexes = new ObservableCollection<IndexRow>();
        private ObservableCollection<Section> _section;
        private ObservableCollection<Category> _cats = new ObservableCollection<Category>();

        public ObservableCollection<Category> Cats
        {
            get
            {
                return _cats;
            }
            set
            {
                _cats = value;
                PropertyChangedInit("Cats");
            }
        }

        public ObservableCollection<Section> Sections
        { 
            get { return _section;}
            set
            {
                _section = value;
                PropertyChangedInit("Sections");
            }
        }

        private ObservableCollection<Department> depTree = new ObservableCollection<Department>();

        public ObservableCollection<Department> DepTree
        {
            get
            {
                return depTree;
            }
            set
            {
                depTree = value;
            }
        }
       
        private IEnumerable<Respondent> GetTeahersFromDep(Department Dep)
        {
            List<Respondent> list = new List<Respondent>();
            foreach (DTOBase Obj in Dep.Childs)
            {
                if (Obj is Respondent)
                {
                    list.Add(Obj as Respondent);
                }
                if (Obj is Department)
                {
                    list.AddRange(GetTeahersFromDep(Obj as Department));
                }
            }
            return list;
        }

        public ObservableCollection<int> BlackTeahers
        {
            get
            {
                ObservableCollection<int> list = new ObservableCollection<int>();
                foreach (Department Dep in depTree)
                {
                    var lst = GetTeahersFromDep(Dep);
                    foreach (var r in lst)
                    {
                        if (r.Check) list.Add(r.Person.IDPerson);
                    }
                }
                return list;
            }
        }


        private void PropertyChangedInit(String PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }

        public IEnumerable<Category> Categories
        {
            get
            {
                return _cats.Where(x => x.IsSelected == true).ToList();
            }
        }


        public VolumsPPSView(IndexStrategyType strategy = IndexStrategyType.PPS)
        {
            StrategyType = strategy;
            InitializeComponent();
            Loaded += new RoutedEventHandler(VolumsPPSView_Loaded);
        }

        void VolumsPPSView_Loaded(object sender, RoutedEventArgs e)
        {
            var vm = MainPage.ViewModel;
            vm.SelectTerm -= ViewModel_SelectTerm;
            vm.SelectTerm += new MainViewModel.SelectTermEventHandler(ViewModel_SelectTerm);
            vm.SelectTermInit();

            client.GetParentsSectionsCompleted += new EventHandler<GetParentsSectionsCompletedEventArgs>(client_GetParentsSectionsCompleted);
            client.GetCategoriesCompleted += new EventHandler<GetCategoriesCompletedEventArgs>(client_GetCategoriesCompleted);
            client.GetUSTUTreeCompleted += new EventHandler<GetUSTUTreeCompletedEventArgs>(client_GetUSTUTreeCompleted);
            client.GetIndexes2Completed +=client_GetIndexesCompleted;
            client.GetAnswersByRespCompleted += new EventHandler<GetAnswersByRespCompletedEventArgs>(client_GetAnswersByRespCompleted);


            client.GetUSTUTreeAsync(termID);
            client.GetParentsSectionsAsync(termID);
            client.GetCategoriesAsync(termID);
        }

        void client_GetIndexesCompleted(object sender, GetIndexes2CompletedEventArgs e)
        {
            _indexes = e.Result;
            IndexGrid.ItemsSource = _indexes;
        }

        void client_GetUSTUTreeCompleted(object sender, GetUSTUTreeCompletedEventArgs e)
        {
            DepTree = e.Result;
            USTUTree.ItemsSource = DepTree;
        }

        void client_GetCategoriesCompleted(object sender, GetCategoriesCompletedEventArgs e)
        {
            _cats=e.Result;
            CategoryLB.ItemsSource = Cats;
        }

        void client_GetParentsSectionsCompleted(object sender, GetParentsSectionsCompletedEventArgs e)
        {
            _section = e.Result;
            
            SectionsTree.ItemsSource = _section;

        }


        void ViewModel_SelectTerm(object sender, SelectTermEventsArgs e)
        {
            TermID = e.SelectTerm.IDTerm;
        }

        private int termID;

        public int TermID
        {
            get
            {
                return termID;
            }
            set
            {
                termID = value;
            }
        }



        private void SelectCats_Click(object sender, RoutedEventArgs e)
        {
            foreach (var c in Cats)
            {
                c.Check = true;
            }
            CategoryLB.ItemsSource = Cats;
            PropertyChangedInit("Cats");
        }

        private void UnselectCats_Click(object sender, RoutedEventArgs e)
        {
            foreach (var c in Cats)
            {
                c.Check = false;
            }
            CategoryLB.ItemsSource = Cats;
            PropertyChangedInit("Cats");
        }

        private void FlushPPS_Click(object sender, RoutedEventArgs e)
        {
           
        }

        private void IndexGrid_LoadingRowGroup(object sender, DataGridRowGroupHeaderEventArgs e)
        {
            e.RowGroupHeader.PropertyNameVisibility = Visibility.Collapsed;
        }


        private void CheckBox_Click(object sender, RoutedEventArgs e)
        {
            client.GetIndexes2Async(termID, null, null,BlackTeahers,  IndexListType.Full, StrategyType);
            client.GetAnswersByRespAsync(20, 2747);
        }

        void client_GetAnswersByRespCompleted(object sender, GetAnswersByRespCompletedEventArgs e)
        {
            //MessageBox.Show("А вот думай черепом!");
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private IndexStrategyType StrategyType;

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {

        }

        private void Image_ImageFailed(object sender, ExceptionRoutedEventArgs e)
        {

        }

        private void SelectSections_Click(object sender, RoutedEventArgs e)
        {
            foreach (var c in Sections)
            {
                c.Check = true;
            }
            SectionsTree.ItemsSource = Sections;
            PropertyChangedInit("Sections");
        }

        private void UnselectSections_Click(object sender, RoutedEventArgs e)
        {
            foreach (var c in Sections)
            {
                c.Check = false;
            }
            SectionsTree.ItemsSource = Sections;
            PropertyChangedInit("Sections");
        }
    }
}
